﻿using System.Windows.Controls;
using System.Windows;
using System.Data.SQLite;
using System.Collections.Generic;
using Ookii.Dialogs.Wpf;

namespace KeyboardImageViewer
{
    public class MultiFolderPickerWithRelocation : MultiFolderPicker
    {
        private List<string> movedDirectories = new List<string>();

        public List<string> MovedDirectories
        {
            get
            {
                return this.movedDirectories;
            }
        }

        protected override StackPanel CreateStackPanel(string text, bool? initialChecked, bool directoryExists)
        {
            StackPanel prevPanel = base.CreateStackPanel(text, initialChecked, directoryExists);
            if (!directoryExists)
            {
                Button specifyLocationButton = new Button { Content = "Specify New Location...", Margin = new Thickness(2) };
                specifyLocationButton.Click += new System.Windows.RoutedEventHandler(specifyLocationButton_Click);

                prevPanel.Children.Add(specifyLocationButton);
            }

            return prevPanel;
        }

        private void specifyLocationButton_Click(object sender, System.Windows.RoutedEventArgs e)
        {
			var folderDialog = new VistaFolderBrowserDialog();
			if (folderDialog.ShowDialog() == true)
			{
				Button clickedButton = sender as Button;
				StackPanel stackPanel = clickedButton.Parent as StackPanel;
				TreeViewItem item = stackPanel.Parent as TreeViewItem;
				string path = item.Tag as string;

				base.AddFolder(folderDialog.SelectedPath);

				this.movedDirectories.Add(path);

				TreeView treeView = item.Parent as TreeView;
				treeView.Items.Remove(item);
			}
        }
    }
}